<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>事件绑定 | 北城韩雨</title><meta name="keywords" content="JavaScript"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="JavaScript 事件绑定">
<meta property="og:type" content="article">
<meta property="og:title" content="事件绑定">
<meta property="og:url" content="http://example.com/2020/04/01/%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="JavaScript 事件绑定">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475">
<meta property="article:published_time" content="2020-03-31T16:00:00.000Z">
<meta property="article:modified_time" content="2019-05-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2020/04/01/%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '事件绑定',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-05-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">事件绑定</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-03-31T16:00:00.000Z" title="发表于 2020-04-01 00:00:00">2020-04-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-05-02T16:00:00.000Z" title="更新于 2019-05-03 00:00:00">2019-05-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/JavaScript/">JavaScript</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>9分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="事件绑定"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="DOM事件"><a href="#DOM事件" class="headerlink" title="DOM事件"></a>DOM事件</h1><h2 id="基础知识"><a href="#基础知识" class="headerlink" title="基础知识"></a>基础知识</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 事件是浏览器赋予元素的默认行为 也可以理解为事件 是天生具备的 不论我们是否为其绑定方法 当某些行为触发的时候 相关的事件都会被执行</span><br><span class="line"><span class="number">2.</span> 事件绑定 给元素默认的事件行为 绑定方法 可以在行为触发的时候 执行这个方法</span><br></pre></td></tr></table></figure>
<h2 id="常用事件"><a href="#常用事件" class="headerlink" title="常用事件"></a>常用事件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 鼠标事件 </span><br><span class="line">click 点击事件(pc) 单击事件(移动端 300ms 内没有发生第二次点击操作 算作单机事件行为 所以click在移动端 有300ms 延迟)</span><br><span class="line">dblclick 双击事件</span><br><span class="line">contextmenu 鼠标右键点击触发</span><br><span class="line">mousedown 鼠标按下 </span><br><span class="line">mouseup 鼠标抬起   </span><br><span class="line">mousemove 鼠标移动</span><br><span class="line">mouseover 鼠标滑入</span><br><span class="line">mouseout 鼠标滑出</span><br><span class="line">mouseenter 鼠标进入</span><br><span class="line">mouseleave 鼠标离开</span><br><span class="line">mousewheel 鼠标滚轮</span><br><span class="line"><span class="number">2.</span> 键盘事件</span><br><span class="line">keydown 键盘按下</span><br><span class="line">keyup 键盘抬起</span><br><span class="line">keypress 长按 除了shift fn capslock</span><br><span class="line"><span class="number">3.</span> 手指事件</span><br><span class="line">「touch event 单手指事件模型」 </span><br><span class="line">touchstart 手指按下</span><br><span class="line">touchmove 手指移动</span><br><span class="line">touchend 手指松开</span><br><span class="line">「Gesture Event 多手指事件模型」</span><br><span class="line"><span class="number">4.</span> 表单事件</span><br><span class="line">focus 获取焦点</span><br><span class="line">blur 失去焦点</span><br><span class="line">submit 提交事件 表单元素都包含在form 并且点击的按钮 是submit</span><br><span class="line">reset 表单重置 表单元素要包含在form</span><br><span class="line">select 下拉框内容选中</span><br><span class="line">change 内容改变</span><br><span class="line">input 移动端经常使用的 监控文本框中的内容随着输入的改变而出发</span><br><span class="line"><span class="number">5.</span> 资源事件</span><br><span class="line">load 加载成功 <span class="built_in">window</span>.onload / img.onload</span><br><span class="line">error 加载失败</span><br><span class="line">beforeunload 资源卸载之前 <span class="built_in">window</span>.onbeforedunload 页面关闭之前触发</span><br><span class="line"><span class="number">6.</span> CSS3动画事件</span><br><span class="line">transitionend transition动画结束</span><br><span class="line">transitionstart transition动画开始</span><br><span class="line">transitionrun transition动画运行中</span><br><span class="line"><span class="number">7.</span>视图事件 </span><br><span class="line">resize 元素 或者浏览器 大小改变</span><br><span class="line">scroll 滚动条滚动</span><br></pre></td></tr></table></figure>
<h2 id="DOM-0-级事件绑定"><a href="#DOM-0-级事件绑定" class="headerlink" title="DOM 0 级事件绑定"></a>DOM 0 级事件绑定</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 语法 元素.on[事件] = [函数] <span class="built_in">document</span>.body.onclick= <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line"><span class="number">2.</span> 移除 赋值为<span class="literal">null</span> 或者其他非函数值即可 <span class="built_in">document</span>.body.onclick =  <span class="literal">null</span></span><br><span class="line"><span class="number">3.</span> 原理 每一个DOM 元素对象的私有属性上 有很多 类似于 onxxx的私有属性 我们给这些代表事件的私有属性赋值 就是DOM0级绑定</span><br><span class="line"><span class="number">4.</span> 如果没有对应事件私有属性(onDOMContentLoaded)则无法基于这种办法给事件赋值 只能给当前某个事件行为 绑定一个方法 如果绑定多个方法 最后一个操作会覆盖以前的</span><br><span class="line"><span class="number">5.</span> 好处是 执行效率快 而且开发者使用起来也很方便</span><br></pre></td></tr></table></figure>
<h2 id="DOM-2-级事件绑定"><a href="#DOM-2-级事件绑定" class="headerlink" title="DOM 2 级事件绑定"></a>DOM 2 级事件绑定</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>语法</span><br><span class="line"><span class="number">2.</span> 一般不是用匿名函数去绑定 因为没办法移除</span><br><span class="line"><span class="number">3.</span> 每一恶搞DOM元素都会基于__proto__ 查找到EventTarget.prototype上的 addEventlistener / removeEventlistener 等方法 基于这些方法实现事件</span><br><span class="line"><span class="number">4.</span> 凡是浏览器提供的事件行为 都可以基于这种模式完成事件绑定和移除</span><br><span class="line"><span class="number">5.</span> 可以绑定多个方法</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[元素].addEventListener([事件],[方法],[捕获/冒泡])</span><br><span class="line"><span class="built_in">document</span>.body.addEventListener(<span class="string">&#x27;click&#x27;</span>,fn1,<span class="literal">false</span>)</span><br><span class="line">移除 但是需要参数和绑定的时候一样</span><br><span class="line">[元素].removeEventListener([事件],[方法],[捕获/冒泡])</span><br></pre></td></tr></table></figure>
<h2 id="事件对象"><a href="#事件对象" class="headerlink" title="事件对象"></a>事件对象</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 给当前元素 的某个事件行为 绑定方法 当事件行为触发 不仅会把方法执行 而且还会给方法默认传递一个实参 这个实参 就是我们的事件对象</span><br><span class="line"><span class="number">2.</span> 事件对象 就是存储当前事件操作及触发的相关信息的 这个信息是浏览器记录的</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//鼠标</span></span><br><span class="line"><span class="keyword">let</span> body = <span class="built_in">document</span>.body</span><br><span class="line">body.onclick = <span class="function"><span class="keyword">function</span> (<span class="params">ev</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(ev)</span><br><span class="line">  <span class="comment">// 输出 鼠标事件对象 MouseEvent 以下会有一些对象下的属性是我们常用的</span></span><br><span class="line">  clientX/clientY 鼠标触发点距离当前窗口的 X / Y 轴坐标</span><br><span class="line">  pageX/pageY 鼠标点距离body的 X / Y 轴的坐标 因为我们的body可能卷出去</span><br><span class="line">  type 事件类型</span><br><span class="line">  target / srcElement 获取当前事件源(当前操作的元素)</span><br><span class="line">  path 记录的是 传播路径</span><br><span class="line">  preventDefault() / returnValur = <span class="literal">false</span> 阻止默认行为</span><br><span class="line">  stopPropagation() / canceBubble = <span class="literal">true</span> 组织冒泡传播</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//键盘</span></span><br><span class="line"><span class="built_in">document</span>.onkeydown = <span class="function"><span class="keyword">function</span> (<span class="params">ev</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(ev)</span><br><span class="line">  <span class="comment">// 输出 键盘事件对象 KeyboardEvent </span></span><br><span class="line">  witch / keyCode 获取键盘按键码</span><br><span class="line">  key 存储按键名称</span><br><span class="line">  altKey 是否按下alt键(主要用来组合按钮使用)</span><br><span class="line">  ctrlKey 是否按下ctrl键(主要用来组合按钮使用)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//手指</span></span><br><span class="line"><span class="built_in">document</span>.body.ontouchstart = <span class="function"><span class="keyword">function</span> (<span class="params">ev</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">let</span> point = ev.changedTouches[<span class="number">0</span>]</span><br><span class="line">  <span class="built_in">console</span>.log(point) <span class="comment">// 是一个对象 里面记录的是第一个手指的信息 比如离屏幕的坐标等等 和pc基本上一致</span></span><br><span class="line">  <span class="built_in">console</span>.log(ev)</span><br><span class="line">  <span class="comment">// 输出手指事件对象(移动端)</span></span><br><span class="line">  changedTouches / targetTouches / touches 都是用来记录手指信息</span><br><span class="line">  changedTouches 手指按下 移动 离开屏幕 都存储了手指的信息 哪怕离开屏幕后 记录最后一次</span><br><span class="line">  touches 同上 不过 最后一次 手指信息 就不会被记录了</span><br><span class="line">  最后获取的结果 获取的都是一个TouchList 记录每一根手指的信息</span><br><span class="line">  ev.changedTouches[<span class="number">0</span>] 第一根手指信息</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="事件对象的组织默认行为"><a href="#事件对象的组织默认行为" class="headerlink" title="事件对象的组织默认行为"></a>事件对象的组织默认行为</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 浏览器会赋予元素很多的默认行为</span><br><span class="line">鼠标右键菜单</span><br><span class="line">点击a标签跳转页面</span><br><span class="line">部分浏览器会记录输入记录 下次再一次输入的时候会有模糊匹配</span><br><span class="line">键盘按下 会输入内容</span><br><span class="line"><span class="number">2.</span> 以上种种默认行为 我们都可以基于 ev.preventDefault() 来阻止这些默认行为</span><br></pre></td></tr></table></figure>
<h2 id="自定义右键菜单"><a href="#自定义右键菜单" class="headerlink" title="自定义右键菜单"></a>自定义右键菜单</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>正常情况我们 我们点击鼠标右键 会弹出浏览器默认的右键菜单 所以我们首先要组织默认行为</span><br><span class="line"><span class="number">2.</span> 然后创建我们的自己的元素 其实很简单 就是 div 和 li 然后 让我们创建的div 定位到我们鼠标的位置 通过事件对象我们可以拿到 鼠标的坐标</span><br><span class="line"><span class="number">3.</span> 然后点击空白出的时候 我们需要销毁我们的自己创建的元素 让菜单消失 这里我们需要判断你点击的是 是不是我们创建的li 如果不是 就让菜单销毁</span><br><span class="line"><span class="number">4.</span> 我们需要判断 你点击的事件源 是不是你的li 如果是 我们就让我们的事件源 和事件标签 等于 我们上一层 也就是ul 直到最后外层 我们就什么也不干</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1. 首先我们要监听鼠标的右键 这里我们需要给body去监听 然后 要组织它的默认行为</span></span><br><span class="line"><span class="comment">// 为了组织浏览器右键自己的菜单出现</span></span><br><span class="line"><span class="built_in">document</span>.body.oncontextmenu = <span class="function"><span class="keyword">function</span> (<span class="params">ev</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 组织浏览器右键自己弹出来菜单</span></span><br><span class="line">    ev.preventDefault()</span><br><span class="line">    <span class="comment">//2. 先看看能不能获取到这个菜单元素</span></span><br><span class="line">    <span class="keyword">let</span> contextmenu = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;#contextmenu&#x27;</span>)</span><br><span class="line">    <span class="comment">//3.如果获取不到 我们就自己创建一个菜单</span></span><br><span class="line">    <span class="keyword">if</span>(!contextmenu)&#123;</span><br><span class="line">        <span class="comment">//创建元素</span></span><br><span class="line">        contextmenu = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">        <span class="comment">//赋值id</span></span><br><span class="line">        contextmenu.id = <span class="string">&#x27;contextmenu&#x27;</span></span><br><span class="line">        <span class="keyword">const</span> html = <span class="string">`&lt;ul&gt;</span></span><br><span class="line"><span class="string">                &lt;li&gt;菜单一&lt;/li&gt;</span></span><br><span class="line"><span class="string">                &lt;li&gt;菜单二&lt;/li&gt;</span></span><br><span class="line"><span class="string">                &lt;li&gt;菜单三&lt;/li&gt;</span></span><br><span class="line"><span class="string">            &lt;/ul&gt;`</span></span><br><span class="line">        contextmenu.innerHTML = html</span><br><span class="line">        <span class="comment">//插入到body中</span></span><br><span class="line">        <span class="built_in">document</span>.body.appendChild(contextmenu)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//获取鼠标的 x y 轴地址</span></span><br><span class="line">    <span class="keyword">const</span> x = ev.clientX</span><br><span class="line">    <span class="keyword">const</span> y = ev.clientY</span><br><span class="line">    <span class="comment">//把菜单的位置 改变</span></span><br><span class="line">    contextmenu.style.top = y+ <span class="number">10</span> + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">    contextmenu.style.left = x + <span class="number">10</span> +<span class="string">&#x27;px&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//4. 点击空白的时候 要移除我们上面创建的元素 这里需要注意</span></span><br><span class="line"><span class="comment">// 首先 我们点空白处 要移除我们创建的元素 </span></span><br><span class="line"><span class="comment">// 其次 我们如果点击到 我们的菜单了 是不能移除了</span></span><br><span class="line"><span class="comment">// 这里点击的时候 我们能拿到它点击的事件源头 比如 你是点击的是也看上的哪个元素</span></span><br><span class="line"><span class="built_in">window</span>.onclick = <span class="function"><span class="keyword">function</span>(<span class="params">ev</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//5.比如此时你点击的是 body 那么就是body 如果是li 那么就是li</span></span><br><span class="line">   <span class="keyword">let</span> target = ev.target</span><br><span class="line">   <span class="comment">//拿到具体的事件源的标签</span></span><br><span class="line">   <span class="keyword">let</span> targetTag = target.tagName</span><br><span class="line">   <span class="comment">//6. 如果你点击的是li 那么我们肯定不能移除元素</span></span><br><span class="line">   <span class="keyword">if</span>(targetTag === <span class="string">&#x27;LI&#x27;</span>)&#123;</span><br><span class="line">       <span class="comment">//我们就让我们的点击事件源向上找一层 是ul</span></span><br><span class="line">       target = targetTag.parentNode</span><br><span class="line">       targetTag = target.tagName</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="comment">//7. 然后我们继续向上找 直到你不我们的创建的元素位置</span></span><br><span class="line">   <span class="keyword">if</span>(targetTag === <span class="string">&#x27;UL&#x27;</span> &amp;&amp; target.parentNode.id === <span class="string">&#x27;contextmenu&#x27;</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">   &#125;</span><br><span class="line">   <span class="comment">//8.否则 让右侧菜单消失 </span></span><br><span class="line">   <span class="keyword">let</span> contextmenu = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;#contextmenu&#x27;</span>)</span><br><span class="line">   <span class="keyword">if</span>(contextmenu)&#123;</span><br><span class="line">       <span class="built_in">document</span>.body.removeChild(contextmenu)</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 代码 地址 https:<span class="comment">//github.com/wazer1987/contextmenu</span></span><br></pre></td></tr></table></figure>
<h2 id="事件传播机制"><a href="#事件传播机制" class="headerlink" title="事件传播机制"></a>事件传播机制</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 我们页面上 有 <span class="built_in">window</span> <span class="built_in">document</span> body html 然后body 里面还有我们写的一些元素</span><br><span class="line"><span class="number">2.</span> 这些元素上 都有浏览器的赋予他们的事件 比如click等等 </span><br><span class="line"><span class="number">3.</span> 直到我们给他赋予事件处理函数的时候 这些事件都会被触发</span><br><span class="line"><span class="number">4.</span> 首先 他会冲 <span class="built_in">window</span> 开始 一层一层向下寻找 看看 是我们哪个元素触发的事件 这个阶段我们 称之为 捕获阶段(CAPTURING_PHASE) 捕获阶段会记录你查找的路径 也就是e.path</span><br><span class="line"><span class="number">5.</span> 其次 找到之后 就会触发我们的事件处理函数 这个阶段我们 称之为 目标阶段(AT_TARGET)</span><br><span class="line"><span class="number">6.</span> 最后 事件处理完毕之后 会按照我们的捕获阶段获取的路径 一层一层在向上触发(如果在这个路径上某个元素绑定了事件处理函数 也会被执行) 这个阶段叫做冒泡(BUBBLING_PHASE)</span><br><span class="line"><span class="number">7.</span> DOM <span class="number">0</span>级 事件绑定中给元素事件行为绑定的方法 都是在目标阶段/冒泡阶段触发的</span><br></pre></td></tr></table></figure>
<p><img src="/Users/笔记/笔记/事件绑定/事件绑定1.jpg" alt=""></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> box = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.box&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> outer = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.outer&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> inner = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.inner&#x27;</span>)</span><br><span class="line">inner.onclick = <span class="function"><span class="keyword">function</span> (<span class="params">ev</span>)</span>&#123;</span><br><span class="line">  ev.stopPropagation()</span><br><span class="line">&#125;</span><br><span class="line">outer.onclick = <span class="function"><span class="keyword">function</span> (<span class="params">ev</span>)</span>&#123;</span><br><span class="line">  ev.stopPropagation()</span><br><span class="line">&#125;</span><br><span class="line">inner.onclick = <span class="function"><span class="keyword">function</span> (<span class="params">ev</span>)</span>&#123;</span><br><span class="line">  ev.stopPropagation()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="事件委托"><a href="#事件委托" class="headerlink" title="事件委托"></a>事件委托</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 以上我们阻止了冒泡 但是这样并不好 最起码写了三个函数 开辟了三个堆内存</span><br><span class="line"><span class="number">2.</span> 因为点击事件存在冒泡传播机制 所以不论点击 inner/outer/box 最后都会传播到BODY上 触发BODY click 事件行为 把为期绑定的方法执行</span><br><span class="line"><span class="number">3.</span> 在方法执行接受到的事件对象中 有一个target属性(事件源) 可以知道当前操作的是谁 我们方法中可以根据事件源的不同 我们做不同的处理</span><br><span class="line"><span class="number">4.</span> 我们把这种机制叫作事件委托 或 事件代理</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.body.click = <span class="function"><span class="keyword">function</span> (<span class="params">ev</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> target = ev.target</span><br><span class="line">  		targeClass = target.ClassName</span><br><span class="line">  <span class="keyword">if</span>(targeClass === <span class="string">&#x27;inner&#x27;</span>) &#123;</span><br><span class="line">     	<span class="built_in">console</span>.log(<span class="string">&#x27;inner&#x27;</span>)</span><br><span class="line">    	<span class="keyword">return</span></span><br><span class="line">   &#125;</span><br><span class="line">  <span class="keyword">if</span>(targeClass === <span class="string">&#x27;outer&#x27;</span>) &#123;</span><br><span class="line">     	<span class="built_in">console</span>.log(<span class="string">&#x27;outer&#x27;</span>)</span><br><span class="line">    	<span class="keyword">return</span></span><br><span class="line">   &#125;</span><br><span class="line">  <span class="keyword">if</span>(targeClass === <span class="string">&#x27;box&#x27;</span>) &#123;</span><br><span class="line">     	<span class="built_in">console</span>.log(<span class="string">&#x27;box&#x27;</span>)</span><br><span class="line">    	<span class="keyword">return</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="鼠标滑入事件的区别"><a href="#鼠标滑入事件的区别" class="headerlink" title="鼠标滑入事件的区别"></a>鼠标滑入事件的区别</h2><h3 id="mouseover-和-mouseout"><a href="#mouseover-和-mouseout" class="headerlink" title="mouseover 和 mouseout"></a>mouseover 和 mouseout</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>假设有盒子A 和 盒子B 盒子B 在盒子 A 里面</span><br><span class="line"><span class="number">2.</span>当我们对B使用 mouseover的时候 会触发事件mouseover </span><br><span class="line"><span class="number">3.</span>当我们鼠滑入到B里面的时候 不太会触发B的mouseover时间 由于冒泡机制 还会触发A的mouseout 事件 </span><br><span class="line"><span class="number">4.</span>当我们鼠标 离开 B 盒子的时候 会触发 B 盒子的mouseout事件 由于冒泡机制的传播 还会触发 A盒子的mouseover事件</span><br></pre></td></tr></table></figure>
<h3 id="mouseenter和mouseleave"><a href="#mouseenter和mouseleave" class="headerlink" title="mouseenter和mouseleave"></a>mouseenter和mouseleave</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>和我们的mouseover 和 mouseout正好相反</span><br><span class="line"><span class="number">2.</span> 如果进入 A和B的重叠区域。不会像上面一样频繁的触发冒泡的传播机制。</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>事件绑定</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2020/04/01/%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A/">http://example.com/2020/04/01/%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2020-04-01</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-05-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaScript/">JavaScript</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/04/13/ssr%20nuxt/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530687&amp;t=c2e3c97de6dbdc29e77593d5f4dc7fb5" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">SSR 之 服务端渲染</div></div></a></div><div class="next-post pull-right"><a href="/2020/03/29/Dialog/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">弹框组件</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/04/27/写一个简版的Promise/" title="手写一个简版的promise"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2017-07-20%2F4f06f9aa2ebcea6691a78537052e8e9f%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530599&t=32fb97bad7007bdc6c712ccadacd5721" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-27</div><div class="title">手写一个简版的promise</div></div></a></div><div><a href="/2020/04/27/设计模式/" title="发布订阅"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-27</div><div class="title">发布订阅</div></div></a></div><div><a href="/2020/04/26/js事件循环机制/" title="JavaScript 事件循环机制"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-26</div><div class="title">JavaScript 事件循环机制</div></div></a></div><div><a href="/2020/04/22/ES6/" title="ES6/ES7/ES8/ES9/ES10/ES11"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-22</div><div class="title">ES6/ES7/ES8/ES9/ES10/ES11</div></div></a></div><div><a href="/2020/03/29/Dialog/" title="弹框组件"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-03-29</div><div class="title">弹框组件</div></div></a></div><div><a href="/2020/03/28/拖拽/" title="拖拽"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530724&t=333c5056a813a542419e5b661ebcfcd9" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-03-28</div><div class="title">拖拽</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#DOM%E4%BA%8B%E4%BB%B6"><span class="toc-text">DOM事件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"><span class="toc-text">基础知识</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E4%BA%8B%E4%BB%B6"><span class="toc-text">常用事件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM-0-%E7%BA%A7%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A"><span class="toc-text">DOM 0 级事件绑定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM-2-%E7%BA%A7%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A"><span class="toc-text">DOM 2 级事件绑定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E5%AF%B9%E8%B1%A1"><span class="toc-text">事件对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%BB%84%E7%BB%87%E9%BB%98%E8%AE%A4%E8%A1%8C%E4%B8%BA"><span class="toc-text">事件对象的组织默认行为</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%B3%E9%94%AE%E8%8F%9C%E5%8D%95"><span class="toc-text">自定义右键菜单</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E4%BC%A0%E6%92%AD%E6%9C%BA%E5%88%B6"><span class="toc-text">事件传播机制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E5%A7%94%E6%89%98"><span class="toc-text">事件委托</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%BC%A0%E6%A0%87%E6%BB%91%E5%85%A5%E4%BA%8B%E4%BB%B6%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">鼠标滑入事件的区别</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#mouseover-%E5%92%8C-mouseout"><span class="toc-text">mouseover 和 mouseout</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#mouseenter%E5%92%8Cmouseleave"><span class="toc-text">mouseenter和mouseleave</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>